// pages/order/order.js
const app = getApp()
var help = require('../../utils/help.js');
var login = require('../../utils/login.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //tab切换 
    tabActive0: "active",
    tabActive1: "",
    tabActive2: "",
    tabActive3: "",
    tabActive4: "",
    tabActive5: "",
    orderActive1: "orderActive",
    orderActive2: "",
    orderActive3: "",
    orderActive4: "",
    orderActive5: "",
    status:-1, // 0=待付款,1=待发货,2=待收货,3=已完成,4=售后,5=已取消

    imageUrl: app.data.imageUrl,

    data: [],
    page: 1, // 当前页
    is_load: true, // 是否继续加载
    load_lock: false, // 加载锁
    load_ts: '',
    keyword: '', // 关键字
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    if (!app.data.userInfo) {
      login.getUserInfo(app);
      return false;
    }
    if (options.status == '0') {
      this.setData({
        status: options.status,
        tabActive0: "",
        tabActive1: "active",
        tabActive2: "",
        tabActive3: "",
        tabActive4: "",
        tabActive5: "",
      })
    } else if (options.status == '1') {
      this.setData({
        status: options.status,
        tabActive0: "",
        tabActive1: "",
        tabActive2: "active",
        tabActive3: "",
        tabActive4: "",
        tabActive5: "",
      })
    } else if (options.status == '2') {
      this.setData({
        status: options.status,
        tabActive0: "",
        tabActive1: "",
        tabActive2: "",
        tabActive3: "active",
        tabActive4: "",
        tabActive5: "",
      })
    } else if (options.status == '3') {
      this.setData({
        status: options.status,
        tabActive0: "",
        tabActive1: "",
        tabActive2: "",
        tabActive3: "",
        tabActive4: "active",
        tabActive5: "",
      })
    } else if (options.status == '4') {
      this.setData({
        status: options.status,
        tabActive0: "",
        tabActive1: "",
        tabActive2: "",
        tabActive3: "",
        tabActive4: "",
        tabActive5: "active",
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    that.loadData(true)
  },

  /**
   * 加载数据
   */
  loadData: function (is_one) {
    var that = this;

    if (is_one === true) {
      that.setData({
        is_load: true
      })
    }
    if (this.data.load_lock || !this.data.is_load) {
      return false;
    }
    this.setData({
      load_lock: true,
      load_ts: '正在加载'
    })
    if (is_one === true) {
      that.setData({
        page: 1,
        // data: []
      })
    }

    var url = app.data.apiUrl + 'Order/orderList';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      status: that.data.status,
      page: that.data.page
    };
    help.get(url, data, that, function (res, that) {
      that.setData({
        load_lock: false
      })
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        if (result.data.length > 0) {
          if (is_one === true) {
            var new_data = result.data;
          } else {
            var new_data = that.data.data.concat(result.data)
          }
          console.log(new_data)
          that.setData({
            data: new_data,
            load_ts: ''
          })
        } else {
          if (is_one === true) {
            that.setData({
              data: []
            })
          }
          that.setData({
            is_load: false,
            load_ts: '没有更多数据了',

          })
        }
        that.data.page++;

      } else {
        help.msg(result.msg)
        return false;
      }

    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.loadData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //tab切换函数
  orderTab0: function (e) {
    this.setData({
      status: -1,
      tabActive0: "active", tabActive1: "", tabActive2: "", tabActive3: "", tabActive4: "", tabActive5: "",
      orderActive0: "orderActive", orderActive1: "", orderActive2: "", orderActive3: "", orderActive4: "", orderActive5: ""
    })
    this.loadData(true)
  },
  orderTab1: function (e) {
    this.setData({
      status:0,
      tabActive0: "", tabActive1: "active", tabActive2: "", tabActive3: "", tabActive4: "", tabActive5: "",
      orderActive1: "orderActive", orderActive2: "", orderActive3: "", orderActive4: "", orderActive5: ""
    })
    this.loadData(true)
  },
  orderTab2: function (e) {
    this.setData({
      status:1,
      tabActive0: "", tabActive1: "", tabActive2: "active", tabActive3: "", tabActive4: "", tabActive5: "",
      orderActive1: "", orderActive2: "orderActive", orderActive3: "", orderActive4: "", orderActive5: ""
    })
    this.loadData(true)
  },
  orderTab3: function (e) {
    this.setData({
      status:2,
      tabActive0: "", tabActive1: "", tabActive2: "", tabActive3: "active", tabActive4: "", tabActive5: "",
      orderActive1: "", orderActive2: "", orderActive3: "orderActive", orderActive4: "", orderActive5: ""
    })
    this.loadData(true)
  },
  orderTab4: function (e) {
    this.setData({
      status:3,
      tabActive0: "", tabActive1: "", tabActive2: "", tabActive3: "", tabActive4: "active", tabActive5: "",
      orderActive1: "", orderActive2: "", orderActive3: "", orderActive4: "orderActive", orderActive5: ""
    })
    this.loadData(true)
  },
  orderTab5: function (e) {
    this.setData({
      status:4,
      tabActive0: "", tabActive1: "", tabActive2: "", tabActive3: "", tabActive4: "", tabActive5: "active",
      orderActive1: "", orderActive2: "", orderActive3: "", orderActive4: "", orderActive5: "orderActive"
    })
    this.loadData(true)
  },

  /**
   * 取消订单
   */
  closeOrder: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要取消订单吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'Order/orderClose';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 申请退款
   */
  tuikuan: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要申请退款吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'Order/tuikuan';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 取消退款
   */
  untuikuan: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要取消退款吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'Order/delkuan';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },
  /**
   * 删除订单
   */
  delOrder: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要删除订单吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'Order/orderDel';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })
  },

  /**
   * 立即付款
   */
  nowpay: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;

    // 微信支付
    var url = app.data.apiUrl + 'Order/wxpay';
    var data = {
      token: app.data.userInfo ? app.data.userInfo.token : '',
      pay_id: id,
    };
    help.get(url, data, that, function (res, that) {
      var result = res.data;
      console.log(result);
      if (result.code == 1) {
        wx.requestPayment({
          'timeStamp': result.data.timeStamp,
          'nonceStr': result.data.nonceStr,
          'package': result.data.package,
          'signType': result.data.signType,
          'paySign': result.data.paySign,
          'success': function (res) {
            console.log(res)
            help.msg('支付成功');
            setTimeout(function () {
              help.goUrl('/pages/order/index/index');
            }, 1000)
            return false;
          },
          'fail': function (res) {
            console.log(res)
            if (res.errMsg == 'requestPayment:fail cancel') {
              help.msg('未完成支付')
            } else {
              help.msg('支付失败！'.res.errMsg)
            }
            setTimeout(function () {
              help.goUrl('/pages/order/index/index');
            }, 1000)
            return false;
          }
        })
      } else {
        help.msg(result.msg)
        setTimeout(function () {
          help.goUrl('/pages/order/index/index');
        }, 1000)
        return false;
      }

    });

  },
  goProInfo: function (e) {
    var id = e.currentTarget.dataset.id;
    help.goUrl('/pages/product/info/info?id=' + id);
  },
  /**
   * 确认收货
   */
  queren: function (e) {
    var that = this;
    console.log(e);
    var id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '提示',
      content: '确定要确认收货吗？',
      success: function (res) {
        if (res.confirm) {
          var url = app.data.apiUrl + 'Order/queren';
          var data = {
            token: app.data.userInfo ? app.data.userInfo.token : '',
            id: id,
          };
          help.get(url, data, that, function (res, that) {
            var result = res.data;
            console.log(result);
            if (result.code == 1) {
              help.msg(result.msg)
              that.loadData(true);
            } else {
              help.msg(result.msg)
              return false;
            }
          }, function (res, that) {
            wx.hideLoading()
            console.log(res);
            help.msg('网络异常！')
          });
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
      }
    })

  },


})